﻿using System;
using System.Text;

namespace EzDesk.Utility.Extensions
{
    public static class DateTimeExtensions
    {
        /// <summary>
        /// 类似 2010-3-21 12:04 格式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ToFullDateTime(this DateTime date)
        {
            return String.Format(
                "{0}-{1}-{2} {3}:{4}",
                date.Year.ToString("D4"),
                date.Month.ToString("D2"),
                date.Day.ToString("D2"),
                date.Hour.ToString("D2"),
                date.Minute.ToString("D2")
                );
        }

        /// <summary>
        /// 类似 3-2 17:32 的格式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ToShortDateTime(this DateTime date)
        {
            return String.Format(
                "{0}-{1} {2}:{3}",
                date.Month.ToString(),
                date.Day.ToString(),
                date.Hour.ToString("D2"),
                date.Minute.ToString("D2")
                );
        }

        /// <summary>
        /// 类似 2004-02-01 的格式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ToFullDate(this DateTime date)
        {
            return String.Format(
                "{0}-{1}-{2}",
                date.Year.ToString("D4"),
                date.Month.ToString("D2"),
                date.Day.ToString("D2")
                );
        }

        /// <summary>
        /// 类似 03:05:33 的格式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ToFullTime(this DateTime date)
        {
            return String.Format(
                "{0}:{1}:{2}",
                date.Hour.ToString("D2"),
                date.Minute.ToString("D2"),
                date.Second.ToString("D2")
                );
        }

        /// <summary>
        /// 类似 星期五 的格式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ToDayOfWeek(this DateTime date)
        {
            string dayOfWeekStr = null;
            switch (date.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    dayOfWeekStr = "星期日";
                    break;
                case DayOfWeek.Monday:
                    dayOfWeekStr = "星期一";
                    break;
                case DayOfWeek.Tuesday:
                    dayOfWeekStr = "星期二";
                    break;
                case DayOfWeek.Wednesday:
                    dayOfWeekStr = "星期三";
                    break;
                case DayOfWeek.Thursday:
                    dayOfWeekStr = "星期四";
                    break;
                case DayOfWeek.Friday:
                    dayOfWeekStr = "星期五";
                    break;
                case DayOfWeek.Saturday:
                    dayOfWeekStr = "星期六";
                    break;
            }
            return dayOfWeekStr;
        }

        /// <summary>
        /// 类似 2011-02-03 星期六  的格式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ToFullDateAndDay(this DateTime date)
        {
            return string.Format("{0} {1}", date.ToFullDate(), date.ToDayOfWeek());
        }

        /// <summary>
        /// 类似 刚刚 昨天 之类比较友好的格式
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToNiceTime(this DateTime dateTime)
        {
            var timespan = DateTime.Now - dateTime;

            var totalSecond = timespan.TotalSeconds;
            if (totalSecond < 60)
            {
                return "刚刚";
            }

            var totalMinute = timespan.TotalMinutes;
            if (totalMinute < 60)
            {
                return string.Format("{0} 分钟前", (int) totalMinute);
            }

            var dayDiff = timespan.Days;
            if (dayDiff == 0)
            {
                return string.Format("{0} 小时前", (int) timespan.TotalHours);
            }
            if (dayDiff == 1)
            {
                return string.Format(
                    "昨天 {0}:{1}",
                    dateTime.Hour.ToString("D2"),
                    dateTime.Minute.ToString("D2")
                    );
            }
            if (dayDiff == 2)
            {
                return string.Format(
                    "前天 {0}:{1}",
                    dateTime.Hour.ToString("D2"),
                    dateTime.Minute.ToString("D2")
                    );
            }
            var yearDiff = (int) (timespan.Days/365);
            if (yearDiff == 0)
            {
                return dateTime.ToShortDateTime();
            }

            return dateTime.ToFullDateTime();
        }
    }
}
